//
//  DYZSelectCarsSeriesController.m
//  Demo
//
//  Created by 毕仲阳 on 16/8/18.
//  Copyright © 2016年 mycompany. All rights reserved.
//

#import "DYZSelectCarsSeriesController.h"
#import "Masonry.h"
#import "DYZSelectCarsTypeController.h"
#import "DSHTTPClient.h"
#import "UIColor+Addition.h"
#import "DYZSelectCarSeriesModel.h"
#import "DYZSelectCarSeriesChildModel.h"
#import "YYModel.h"
static NSString *carSeriesID = @"carSeriesID";
@interface DYZSelectCarsSeriesController ()
@property(nonatomic,copy)NSArray *carsSeriers;
@end

@implementation DYZSelectCarsSeriesController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self series];
    self.navigationItem.title = @"选择车系";
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:carSeriesID];
    //    NSArray *carsSeriies = @[@{@"name1":
    //                                   @[@{@"title":@"奔驰C级"},@{@"title":@"奔驰E级"},@{@"title":@"奔驰GLA级"},@{@"title":@"奔驰GLK级"}]},
    //                             @{@"name2":
    //                                   @[@{@"title":@"奔驰A级"},@{@"title":@"奔驰B级"},@{@"title":@"奔驰CLA级"},@{@"title":@"奔驰CLS级"},
    //                                     @{@"title":@"奔驰C级(进口)"},
    //                                     @{@"title":@"奔驰CLS级"},
    //                                     @{@"title":@"奔驰CLS级"}]}];
    //    self.carsSeriers = carsSeriies;
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

//解析车系
- (void)series{
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    
    [param setValue:@"1" forKey:@"accountid"];
    [param setValue:self.brandId forKey:@"brandId"];
    //    NSLog(@"%@",self.brandId);
    [DSHTTPClient postUrlString:@"http://iosapi.itcast.cn/car/series.json.php" withParam:param withSuccessBlock:^(id data) {
        
        NSMutableArray *array = [NSMutableArray array];
        
        NSDictionary *dict = [data objectForKey:@"data"];
        
        NSArray *arr = [dict objectForKey:@"carList"];
        
        for (NSDictionary *dict1 in arr) {
            
            DYZSelectCarSeriesModel *model = [DYZSelectCarSeriesModel yy_modelWithDictionary:dict1];
            
            [array addObject:model];
        }
        
        self.carsSeriers = array;
        [self.tableView reloadData];
        
    } withFailedBlock:^(NSError *error) {
        NSLog(@"%@",error);
    } withErrorBlock:^(NSString *message) {
        NSLog(@"%@",message);
    }];
    
}

#pragma mark - Table view data source
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    //    if (section == 0) {
    //        return @"fffff";
    //    }else
    //    {
    //        return @"奔驰进口";
    //    }
    DYZSelectCarSeriesModel *model = self.carsSeriers[section];
    return model.label;
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0,self.tableView.bounds.size.width , 30)];
    headerView.backgroundColor = [UIColor whiteColor];
    UILabel *headerLable = [[UILabel alloc]init];
    headerLable.font = [UIFont systemFontOfSize:14];
    headerLable.textColor = [UIColor grayColor];
    [headerLable sizeToFit];
    //    if (section == 0) {
    //        headerLable.text = @"北京奔驰";
    //    }else
    //    {
    //       headerLable.text = @"奔驰进口";
    //    }
    
    [headerView addSubview:headerLable];
    [headerLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.offset(10);
        
    }];
    
    DYZSelectCarSeriesModel *model = self.carsSeriers[section];
    headerLable.text = model.label;
    return headerView;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.carsSeriers.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    //    if (section == 0) {
    //        return [self.carsSeriers[section][@"name1"] count];
    //    }else
    //    {
    //        return [self.carsSeriers[section][@"name2"] count];
    //    }
    DYZSelectCarSeriesModel *model = self.carsSeriers[section];
    
    return  model.carlineList.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:carSeriesID forIndexPath:indexPath];
    DYZSelectCarSeriesModel *model = self.carsSeriers[indexPath.section];
    DYZSelectCarSeriesChildModel *childModel = model.carlineList[indexPath.row];
    if (indexPath.section == 0 && [self.brandId isEqual:@"BD1001"]) {
        cell.textLabel.text = childModel.carLineName;
    }else{
        cell.textLabel.text = [self.name stringByAppendingFormat:@"%@",childModel.carLineName];
    }
    
    [cell.textLabel setTextColor:[UIColor colorWithHex:0x333333]];
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    //    if (indexPath.section == 0) {
    //        cell.textLabel.text = self.carsSeriers[@"北京奔驰"];
    //    }
    
    //    if (indexPath.section == 0) {
    //        NSArray *arr1 = self.carsSeriers[indexPath.section][@"name1"];
    //
    //        cell.textLabel.text = arr1[indexPath.row][@"title"];
    //
    //    }else
    //    {
    //        NSArray *arr2 = self.carsSeriers[indexPath.section][@"name2"];
    //        cell.textLabel.text = arr2[indexPath.row][@"title"];
    //    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DYZSelectCarsTypeController *ct = [[DYZSelectCarsTypeController alloc]init];
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
    self.navigationItem.backBarButtonItem = item;
    //    [self.navigationController.navigationBar setBarTintColor:[UIColor darkGrayColor]];
    DYZSelectCarSeriesModel *model = self.carsSeriers[indexPath.section];
    DYZSelectCarSeriesChildModel *childModel = model.carlineList[indexPath.row];
    
    ct.carId = childModel.carLineId;
    ct.label = self.name;
    
    [self.navigationController.navigationBar setTintColor:[UIColor darkGrayColor]];
    [self.navigationController pushViewController:ct animated:YES];
}

@end
